const express = require('express')
// const moment = require('moment')
const router = express.Router()
const db = require('../core/newlist.js')

// 查询接口
router.get('/newsget', (req, res) => {
  console.log('------', req.query)
  const {
    id,
    author
  } = req.query
  // const { sex } = req.body
  // select * from 表名称 name = xxx and pwd = xxx
  let sqlStr = ''
  if (!!id && !!author) {
    sqlStr = `SELECT * FROM newslist WHERE state=1 and n_id="${id}" and n_author="${author}"`
  }else if (!id && !!author){
    sqlStr = `SELECT * FROM newslist WHERE state=1 and n_author="${author}"`
  }else if (!!id && !author){
    sqlStr = `SELECT * FROM newslist WHERE state=1 and n_id="${id}"`
  }else if (!id && !author){
    sqlStr = `SELECT * FROM newslist WHERE state=1`
  }
  db.query(sqlStr, (err, result) => {
    console.log('------err', err)
    console.log('------result', result)
    if (err) {
      res.json({
        msg: '服务器错误',
        code: 500
      })
      return
    }
    if (result.length >= 1) {
      res.json({
        msg: '查询成功',
        code: 200,
        data: result
      })
    } else {
      
      res.json({
        msg: '参数错误',
        code: 201
      })
    }
  })
})
// 新增
router.post('/newsadd', (req, res) => {
  console.log('------post', req.query)
  const {
    title,
    context,
    author
  } = req.query
  const sqlStr = `INSERT INTO newslist (n_title,n_context,n_author) VALUES('${title}','${context}','${author}');`
  db.query(sqlStr, (err, result) => {
    console.log('------err', err)
    console.log('------result', result)
    if (err) {
      res.json({
        msg: '服务器错误',
        code: 500
      })
      return
    }
    if (result.affectedRows >= 1) {
      res.json({
        msg: '操作成功',
        code: 200,
        data: []
      })
    } else {
      res.json({
        msg: '参数错误',
        code: 201
      })
    }
  })
})
// 删除
router.post('/newsremove', (req, res) => {
  const {
    id,
    state
  } = req.query
  //   INSERT INTO `students` (`s_name`,`s_age`,`s_sex`,`s_create_time`,`c_id`)
  // VALUES('林正英',22,'男','2020-9-9 10:35:02',1)
  const sqlStr = `UPDATE newslist SET state = 2 WHERE n_id = ${id}`
  db.query(sqlStr, (err, result) => {
    console.log('------result', result)
    if (err) {
      res.json({
        msg: '服务器错误',
        code: 500
      })
      return
    }
    if (result.affectedRows >= 1) {
      res.json({
        msg: '操作成功',
        code: 200,
        data: []
      })
    } else {
      res.json({
        msg: '参数错误',
        code: 201
      })
    }
  })
})
// 改
router.post('/newsupdate', (req, res) => {

  const {
    id,
    title,
    context,
    author
  } = req.query
  // const
  const sqlStr = `UPDATE newslist SET n_title = '${title}',n_context='${context}',n_author='${author}' WHERE n_id = '${id}';`
  db.query(sqlStr, (err, result) => {
    console.log('------err', err)
    console.log('------result', result)
    if (err) {
      res.json({
        msg: '服务器错误',
        code: 500
      })
      return
    }
    if (result.affectedRows >= 1) {
      res.json({
        msg: '操作成功',
        code: 200,
        data: []
      })
    } else {
      res.json({
        msg: '参数错误',
        code: 201
      })
    }
  })
})
module.exports = router